//
//  ErWeiMaView.m
//  DriverClient
//
//  Created by 吕晶 on 2020/11/13.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ErWeiMaView.h"

@interface ErWeiMaView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *imgView;

@end

@implementation ErWeiMaView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        self.backgroundColor = HexColorA(0x000000, 0.5);
        UIButton *btn = [[UIButton alloc] initWithFrame:self.frame];
        [btn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(55, (ScreenH - (ScreenW - 110)) * 0.5, ScreenW - 110, ScreenW - 110)];
    self.bgView.backgroundColor = HexColor(0xffffff);
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 6;
    [self addSubview:self.bgView];
    
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, self.bgView.nim_width - 8, self.bgView.nim_height - 8)];
    [self.bgView addSubview:self.imgView];
}

- (void)showInView:(UIView *)view {
    if (!view) {
        return;
    }
    [view addSubview:self];
    [view addSubview:self.bgView];
}

- (void)disMissView {
    [self removeFromSuperview];
    [self.bgView removeFromSuperview];
}

-(void)setO_id:(int)o_id {
    _o_id = o_id;
    self.imgView.image = [self createCoreImage:o_id];
}

//生成二维码
- (UIImage *)createCoreImage:(int)o_id {
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@(o_id) forKey:@"o_id"];
//    NSString *dataString = [NSString stringWithFormat:@"prev_osd_id=%d",osd_id];
//    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSData *data =   [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    [filter setValue:data forKeyPath:@"inputMessage"];
    //设置纠错级别
    /*
     修正容量 L水平 7%的字码可被修正
     M水平 15%的字码可被修正
     Q水平 25%的字码可被修正
     H水平 30%的字码可被修正
     */
    [filter setValue:@"M" forKey:@"inputCorrectionLevel"];
    CIImage *outputImage = [filter outputImage];
    return [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:80];
}

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


@end
